package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lv.common.bean.CommonSelectedBean
import com.lv.common.imp.OnPositionTListener
import com.lv.service.R
import com.lv.service.adapter.RecyclerSelectedAddressTagAdapter
import com.lv.service.adapter.RecyclerSelectedTag2Adapter
import com.lv.service.bean.TelSecondCategoryBean
import com.lv.service.room.address.ReceiptAddressItem
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：centre=1时，left 点击位置  isshow 选中位置
 */
class AddressLRTopPopupView(
    private val mContext: Context,
    private val mAllBeans: MutableList<ReceiptAddressItem>,
    private val positionListener: OnPositionTListener<MutableList<ReceiptAddressItem>>? = null,

    ) :
    PartShadowPopupView(mContext) {
    private var mLeftAdapter: RecyclerSelectedAddressTagAdapter? = null
    private var mRightAdapter: RecyclerSelectedTag2Adapter? = null
    private var mRvLeft: RecyclerView? = null
    private var mRvRight: RecyclerView? = null
    private var mTopTextName: TextView? = null
    private var mTvLeft: TextView? = null
    private var mTvRight: TextView? = null
    private val mCityBeans = mutableListOf<CommonSelectedBean>()
    private var viewBottom: View? = null
    private var mCurrent = -1

    override fun getImplLayoutId(): Int {
        return R.layout.address_lr_top_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewBottom = findViewById<View>(R.id.view_bottom)
        mRvLeft = findViewById<RecyclerView>(R.id.rv_left)
        mRvRight = findViewById<RecyclerView>(R.id.rv_right)
        mTvLeft = findViewById<TextView>(R.id.tv_left)
        mTvRight = findViewById<TextView>(R.id.tv_right)
        initView()
        initEvnet()
    }

    private fun initView() {
        mCurrent = mAllBeans.indexOfFirst { it.isShow }
        if (mCurrent < 0) {
            mCurrent=0
        }
        mAllBeans.forEach { it.centre = "" }
        mAllBeans[mCurrent].centre = "1"
        mLeftAdapter = RecyclerSelectedAddressTagAdapter(mAllBeans)
        mRvLeft?.adapter = mLeftAdapter


        mRightAdapter = RecyclerSelectedTag2Adapter(mCityBeans, R.layout.item_selected_tag_43)
        mRvRight?.adapter = mRightAdapter
        val topHeader = View.inflate(context, R.layout.item_text_header, null)
        mTopTextName = topHeader.findViewById<TextView>(R.id.tv_name)
        mRightAdapter?.addHeaderView(topHeader)
        initRightDatas(mCurrent)
        val layoutManager = mRvLeft?.layoutManager as LinearLayoutManager
        layoutManager.scrollToPositionWithOffset(mCurrent, 0)

        mLeftAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            if (mCurrent != -1) {
                mAllBeans[mCurrent].centre = ""
                mLeftAdapter?.notifyItemChanged(mCurrent, R.id.tv_name)
            }
            mAllBeans.forEach { it.centre = "" }
            mAllBeans[position].centre = "1"
            mCurrent = position
            mLeftAdapter?.notifyItemChanged(mCurrent, R.id.tv_name)
            initRightDatas(mCurrent)
        }
        mRightAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            val selected = mCityBeans[position - 1].selected
            mCityBeans[position - 1].selected = !selected
            mRightAdapter?.notifyItemChanged(position, R.id.tv_name)
            if (selected) {
                val indexSelected = mCityBeans.indexOfFirst { it.selected }
                if (indexSelected < 0) {
                    if (mCurrent != -1) {
                        mAllBeans[mCurrent].isShow = false
                        mLeftAdapter?.notifyItemChanged(mCurrent, R.id.tv_name)
                    }
                }
            } else {
                if (position == 1) {
                    mCityBeans.forEach { it.selected = false }
                    mCityBeans[0].selected = true
                    mRightAdapter?.notifyDataSetChanged()
                } else {
                    if (mCityBeans[0].selected) {
                        mCityBeans[0].selected = false
                        mRightAdapter?.notifyItemChanged(1, R.id.tv_name)
                    }
                }
                if (mCurrent != -1) {
                    val show = mAllBeans[mCurrent].isShow
                    if (!show) {
                        mAllBeans[mCurrent].isShow = true
                        mLeftAdapter?.notifyItemChanged(mCurrent, R.id.tv_name)
                    }
                }
            }

            val sec = mutableListOf<TelSecondCategoryBean>()
            mCityBeans.forEach {
                sec.add(
                    TelSecondCategoryBean(
                        "", it.name, "", "", "", 0, "",
                        it.selected, null
                    )
                )
            }
            if (mCurrent != -1) {
                mAllBeans[mCurrent].telSecondCategories = sec
            }
        }


    }

    private fun initRightDatas(index: Int) {
        val telSecondCategories = mAllBeans[index].telSecondCategories
        val name = mAllBeans[index].name
        mTopTextName?.text = name
        mCityBeans.clear()
        telSecondCategories?.forEach {
            mCityBeans.add(CommonSelectedBean(it.name, selected = it.isShow))
        }
        mRightAdapter?.notifyDataSetChanged()
    }

    private fun initEvnet() {
        mTvLeft?.setOnClickListener {
            reLoadDatas()
        }

        viewBottom?.setOnClickListener {
            reLoadDatas()
            positionListener?.onClick(-1, mAllBeans)
            dismiss ()
        }
        mTvRight?.setOnClickListener {
            positionListener?.onClick(0, mAllBeans)
            dismiss()
        }
    }

    private fun reLoadDatas() {
        mAllBeans.forEach {
            it.isShow = false
            it.telSecondCategories?.forEach { item -> item.isShow = false }
        }
        mCityBeans.forEach { it.selected = false }
        mLeftAdapter?.notifyDataSetChanged()
        mRightAdapter?.notifyDataSetChanged()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()
//        reLoadDatas()
    }


}